/**
 * 
 */
package gamePlay;

import gui.GUI_manager;
import gui.LoaderGif;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;

import Utils.Concurrent.AsyncTask;
import Utils.Concurrent.LoaderRunner;
import Utils.Concurrent.ThreadTasks.NextChainLongTask;

import main.Session;
import datatype.Question;

/**
 * @author Yuval
 *  * extends the AbsGame, and add some fields and functions for the
 *         Level type, and also create the thread for fetching the questions if
 *         Needed
 */
public class LongMod extends AbsGame {

	private long timeOfLoading = 0;
	private long startStop = 0;
	private List<Question> nextQuestions = null;
	private NextChainLongTask nextQuestionsTask;
	private LoaderGif lgForStart = new LoaderGif(Session.gui.getStart_game_loadingCNVS(), 1);
	private LoaderGif lgForAll = new LoaderGif(Session.gui.getLoadingQuestion_loadingCNVS(), 1);
	//private LoaderRunner loaderR;

	public synchronized List<Question> getNextQuestions() {
		return nextQuestions;
	}

	public synchronized void setNextQuestions(List<Question> nextQuestions) {
		this.nextQuestions = nextQuestions;
	}



	public LongMod() {
		super();

		AsyncGetNextQuestions();

	}

	@Override
	public Question nextQuestion() {
		if (!isAskForMore && chain.getRatio() >= 0.5) {
			isAskForMore = true;
			AsyncGetNextQuestions();
		}

		return chain.nextQuestion();
	}

	@Override
	public void finishGame() {
		super.finishGame();
		stats.setTotlaTime(stats.getTotlaTime() - timeOfLoading);
	}

	public void stop() {
		startStop = System.currentTimeMillis();
	}

	public Question resume() {
		chain.getQuestions().addAll(nextQuestions);
		isAskForMore = false;
		nextQuestions = null;
		if (startStop != 0)
			timeOfLoading += (System.currentTimeMillis() - startStop);
		startStop = 0;
		return nextQuestion();
	}

	public boolean isNextReady() {
		return nextQuestions != null;
	}

	public void prapereChain() {
		chain = new ChainOfLong(nextQuestions);
	}
	
	public void killTask(){
		//Session_new.poolTheQuestions.killAll(loaderR); //TODO
		if(nextQuestionsTask != null)
			Session.poolTheQuestions.killAll(nextQuestionsTask);
	}

	private void AsyncGetNextQuestions() {

		GUI_manager gui = Session.gui;

		// NextChainTask nextQuestionsTask = new NextChainTask(gui.getDisplay(),
		// null);

		String loadingSTR = (chain == null) ? "Loading game, please wait"
				: "Loading next questions, please wait";
//		Label label = (chain == null) ? gui.getLoadingQuestion_loadingLBL() : gui 
	//			.getLoadingQuestion_loadingLBL();
		LoaderGif lg = (chain == null) ? lgForStart : lgForAll;
		Button continueButton = (chain == null) ? gui.getStart_game_startBTN() : gui
				.getLoadingQuestion_continue();

	//	loaderR = new LoaderRunner(gui.getDisplay(), label,
	//			loadingSTR);
		nextQuestionsTask = new NextChainLongTask(Session.gui.getDisplay(),
				 continueButton, lg);
		continueButton.setVisible(false);
		//System.out.println("continueButton is set to be false");
		
		Session.poolTheQuestions.pushUrgentTask(nextQuestionsTask,lg.getLoaderTask());
		//Session_new.poolTheQuestions.addTaskToPool(true, nextQuestionsTask);
		//Session_new.poolTheQuestions.addTaskToPool(true, loaderR);
	}

}
